home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / tcpsubr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  6.8 KB  |  316 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "timer.h"
  4. #include "mbuf.h"
  5. #include "netuser.h"
  6. #include "internet.h"
  7. #include "tcp.h"
  8. #include "ip.h"
  9.  
  10. static int16 hash_tcb __ARGS((struct connection *conn));
  11.  
  12. /* TCP connection states */
  13. char *Tcpstates[] = {
  14.     "",
  15.     "Closed",
  16.     "Listen",
  17.     "SYN sent",
  18.     "SYN received",
  19.     "Established",
  20.     "FIN wait 1",
  21.     "FIN wait 2",
  22.     "Close wait",
  23.     "Last ACK",
  24.     "Closing",
  25.     "Time wait"
  26. };
  27.  
  28. /* TCP closing reasons */
  29. char *Tcpreasons[] = {
  30.     "Normal",
  31.     "Reset/Refused",
  32.     "Timeout",
  33.     "ICMP"
  34. };
  35. struct tcb *Tcbs[NTCB];
  36. int16 Tcp_mss = DEF_MSS;    /* Maximum segment size to be sent with SYN */
  37. int32 Tcp_irtt = DEF_RTT;    /* Initial guess at round trip time */
  38. int Tcp_trace;            /* State change tracing flag */
  39. struct tcp_rtt Tcp_rtt[RTTCACHE];
  40. struct mib_entry Tcp_mib[] = {
  41.     NULLCHAR,        0,
  42.     "tcpRtoAlgorithm",    4,    /* Van Jacobsen's algorithm */
  43.     "tcpRtoMin",        MSPTICK,    /* No lower bound */
  44.     "tcpRtoMax",        MAXINT32,    /* No upper bound */
  45.     "tcpMaxConn",        -1L,    /* No limit */
  46.     "tcpActiveOpens",    0,
  47.     "tcpPassiveOpens",    0,
  48.     "tcpAttemptFails",    0,
  49.     "tcpEstabResets",    0,
  50.     "tcpCurrEstab",        0,
  51.     "tcpInSegs",        0,
  52.     "tcpOutSegs",        0,
  53.     "tcpRetransSegs",    0,
  54.     NULLCHAR,        0,    /* Connection state goes here */
  55.     "tcpInErrs",        0,
  56.     "tcpOutRsts",        0,
  57.     "tcpChkSumErrs",    0,
  58. };
  59.  
  60.  
  61. /* Lookup connection, return TCB pointer or NULLTCB if nonexistant */
  62. struct tcb *
  63. lookup_tcb(conn)
  64. struct connection *conn;
  65. {
  66.     register struct tcb *tcb;
  67.  
  68.     tcb = Tcbs[hash_tcb(conn)];
  69.     while(tcb != NULLTCB){
  70.         /* Yet another structure compatibility hack */
  71.         if(conn->local.address == tcb->conn.local.address
  72.          && conn->remote.address == tcb->conn.remote.address
  73.          && conn->local.port == tcb->conn.local.port
  74.          && conn->remote.port == tcb->conn.remote.port)
  75.             break;
  76.         tcb = tcb->next;
  77.     }
  78.     return tcb;
  79. }
  80.  
  81. /* Create a TCB, return pointer. Return pointer if TCB already exists. */
  82. struct tcb *
  83. create_tcb(conn)
  84. struct connection *conn;
  85. {
  86.     register struct tcb *tcb;
  87.     struct tcp_rtt *tp;
  88.  
  89.     if((tcb = lookup_tcb(conn)) != NULLTCB)
  90.         return tcb;
  91.     tcb = (struct tcb *)callocw(1,sizeof (struct tcb));
  92.     ASSIGN(tcb->conn,*conn);
  93.  
  94.     tcb->state = TCP_CLOSED;
  95.     tcb->cwind = tcb->mss = Tcp_mss;
  96.     tcb->ssthresh = 65535;
  97.     if((tp = rtt_get(tcb->conn.remote.address)) != NULLRTT){
  98.         tcb->srtt = tp->srtt;
  99.         tcb->mdev = tp->mdev;
  100.     } else {
  101.         tcb->srtt = Tcp_irtt;    /* mdev = 0 */
  102.     }
  103.     /* Initialize timer intervals */
  104.     tcb->timer.start = tcb->srtt / MSPTICK;
  105.     tcb->timer.func = tcp_timeout;
  106.     tcb->timer.arg = tcb;
  107.  
  108.     link_tcb(tcb);
  109.     return tcb;
  110. }
  111.  
  112. /* Close our TCB */
  113. void
  114. close_self(tcb,reason)
  115. register struct tcb *tcb;
  116. int reason;
  117. {
  118.     struct reseq *rp1;
  119.     register struct reseq *rp;
  120.  
  121.     if(tcb == NULLTCB)
  122.         return;
  123.  
  124.     stop_timer(&tcb->timer);
  125.     tcb->reason = reason;
  126.  
  127.     /* Flush reassembly queue; nothing more can arrive */
  128.     for(rp = tcb->reseq;rp != NULLRESEQ;rp = rp1){
  129.         rp1 = rp->next;
  130.         free_p(rp->bp);
  131.         free((char *)rp);
  132.     }
  133.     tcb->reseq = NULLRESEQ;
  134.     setstate(tcb,TCP_CLOSED);
  135. }
  136.  
  137. /* Sequence number comparisons
  138.  * Return true if x is between low and high inclusive,
  139.  * false otherwise
  140.  */
  141. int
  142. seq_within(x,low,high)
  143. register int32 x,low,high;
  144. {
  145.     if(low <= high){
  146.         if(low <= x && x <= high)
  147.             return 1;
  148.     } else {
  149.         if(low >= x && x >= high)
  150.             return 1;
  151.     }
  152.     return 0;
  153. }
  154. int
  155. seq_lt(x,y)
  156. register int32 x,y;
  157. {
  158.     return (long)(x-y) < 0;
  159. }
  160. #ifdef    notdef
  161. int
  162. seq_le(x,y)
  163. register int32 x,y;
  164. {
  165.     return (long)(x-y) <= 0;
  166. }
  167. #endif    /* notdef */
  168. int
  169. seq_gt(x,y)
  170. register int32 x,y;
  171. {
  172.     return (long)(x-y) > 0;
  173. }
  174. int
  175. seq_ge(x,y)
  176. register int32 x,y;
  177. {
  178.     return (long)(x-y) >= 0;
  179. }
  180.  
  181. /* Hash a connect structure into the hash chain header array */
  182. static int16
  183. hash_tcb(conn)
  184. struct connection *conn;
  185. {
  186.     register int16 hval;
  187.  
  188.     /* Compute hash function on connection structure */
  189.     hval = hiword(conn->remote.address);
  190.     hval ^= loword(conn->remote.address);
  191. #ifdef    notdef    /* Never changes, so not really needed */
  192.     hval ^= hiword(conn->local.address);
  193.     hval ^= loword(conn->local.address);
  194. #endif
  195.     hval ^= conn->remote.port;
  196.     hval ^= conn->local.port;
  197.     return (int16)(hval % NTCB);
  198. }
  199. /* Insert TCB at head of proper hash chain */
  200. void
  201. link_tcb(tcb)
  202. register struct tcb *tcb;
  203. {
  204.     register struct tcb **tcbhead;
  205.  
  206.     tcb->prev = NULLTCB;
  207.     tcbhead = &Tcbs[hash_tcb(&tcb->conn)];
  208.     tcb->next = *tcbhead;
  209.     if(tcb->next != NULLTCB)
  210.         tcb->next->prev = tcb;
  211.  
  212.     *tcbhead = tcb;
  213. }
  214. /* Remove TCB from whatever hash chain it may be on */
  215. void
  216. unlink_tcb(tcb)
  217. register struct tcb *tcb;
  218. {
  219.     register struct tcb **tcbhead;
  220.  
  221.     tcbhead = &Tcbs[hash_tcb(&tcb->conn)];
  222.     if(tcb->prev == NULLTCB)
  223.         *tcbhead = tcb->next;    /* We're the first one on the chain */
  224.     else
  225.         tcb->prev->next = tcb->next;
  226.     if(tcb->next != NULLTCB)
  227.         tcb->next->prev = tcb->prev;
  228. }
  229. void
  230. setstate(tcb,newstate)
  231. register struct tcb *tcb;
  232. register int newstate;
  233. {
  234.     register char oldstate;
  235.  
  236.     oldstate = tcb->state;
  237.     tcb->state = newstate;
  238.     if(Tcp_trace)
  239.         printf("TCB %lx %s -> %s\n",ptol(tcb),
  240.          Tcpstates[oldstate],Tcpstates[newstate]);
  241.  
  242.     /* Update MIB variables */
  243.     if(oldstate == TCP_CLOSED && newstate == TCP_SYN_SENT)
  244.         tcpActiveOpens++;
  245.     else if(oldstate == TCP_LISTEN && newstate == TCP_SYN_RECEIVED)
  246.         tcpPassiveOpens++;
  247.     else if((oldstate == TCP_SYN_SENT || oldstate == TCP_SYN_RECEIVED)
  248.      && newstate == TCP_CLOSED)
  249.         tcpAttemptFails++; 
  250.     else if(oldstate == TCP_SYN_RECEIVED && newstate == TCP_LISTEN)
  251.         tcpAttemptFails++; 
  252.     else if((oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
  253.      && newstate == TCP_CLOSED)
  254.         tcpEstabResets++;
  255.  
  256.     if(oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
  257.         tcpCurrEstab--;
  258.  
  259.     if(newstate == TCP_ESTABLISHED || newstate == TCP_CLOSE_WAIT)
  260.         tcpCurrEstab++;
  261.  
  262.     if(tcb->s_upcall)
  263.         (*tcb->s_upcall)(tcb,oldstate,newstate);
  264.  
  265.     switch(newstate){
  266.     case TCP_ESTABLISHED:
  267.         /* Notify the user that he can begin sending data */
  268.         if(tcb->t_upcall)
  269.             (*tcb->t_upcall)(tcb,tcb->window - tcb->sndcnt);
  270.         break;
  271.     }
  272. }
  273. /* Round trip timing cache routines.
  274.  * These functions implement a very simple system for keeping track of
  275.  * network performance for future use in new connections.
  276.  * The emphasis here is on speed of update (rather than optimum cache hit
  277.  * ratio) since rtt_add is called every time a TCP connection updates
  278.  * its round trip estimate.
  279.  */
  280. void
  281. rtt_add(addr,rtt)
  282. int32 addr;        /* Destination IP address */
  283. int32 rtt;
  284. {
  285.     register struct tcp_rtt *tp;
  286.     int32 abserr;
  287.  
  288.     if(addr == 0)
  289.         return;
  290.     tp = &Tcp_rtt[(unsigned short)addr % RTTCACHE];
  291.     if(tp->addr != addr){
  292.         /* New entry */
  293.         tp->addr = addr;
  294.         tp->srtt = rtt;
  295.         tp->mdev = 0;
  296.     } else {
  297.         /* Run our own SRTT and MDEV integrators, with rounding */
  298.         abserr = (rtt > tp->srtt) ? rtt - tp->srtt : tp->srtt - rtt;
  299.         tp->srtt = ((AGAIN-1)*tp->srtt + rtt + (AGAIN/2)) >> LAGAIN;
  300.         tp->mdev = ((DGAIN-1)*tp->mdev + abserr + (DGAIN/2)) >> LDGAIN;
  301.     }
  302. }
  303. struct tcp_rtt *
  304. rtt_get(addr)
  305. int32 addr;
  306. {
  307.     register struct tcp_rtt *tp;
  308.  
  309.     if(addr == 0)
  310.         return NULLRTT;
  311.     tp = &Tcp_rtt[(unsigned short)addr % RTTCACHE];
  312.     if(tp->addr != addr)
  313.         return NULLRTT;
  314.     return tp;
  315. }
  316.